package com.glodon.cad.paper.controller;

import com.glodon.cad.paper.pojo.User;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * @author: zhouj-j
 * @version: 2018/4/17 9:34
 */
@Controller
@RequestMapping("/hello")
public class HelloController {

    @Value("${com.glodon.cad.oss.endPoint}")
    private String ossEndPoint;
    
    private final Logger logger = LoggerFactory.getLogger(HelloController.class);

    @RequestMapping(value = "/html", method = RequestMethod.GET)
    public String helloHtml(Model model, @RequestParam("name") String name) {
        model.addAttribute("name", name);
        return "hello";
    }

    @RequestMapping(value = "/json", method = RequestMethod.GET)
    @ResponseBody
    public User helloJson(@RequestParam("name") String name, @RequestParam("age") Integer age) {
        User user = new User();
        user.setAge(age);
        user.setName(name);
        return user;
    }

    @RequestMapping(value = "/custom", method = RequestMethod.GET)
    @ResponseBody
    public User helloJson(@RequestParam("user") User user) {
        logger.info("receive request with param: {}", user);
        if ("123".equals(user.getName())) {
            return user;
        } else {
            logger.error("invalid param: {}, reason: user.name:{} is not equal to 123", user, user.getName());
            return new User();
        }
    }
}
